package com.itchen.school.common.jdkext.lambda;


import com.itchen.school.common.jdkext.Function2;
import com.google.common.collect.Lists;

import java.util.Iterator;
import java.util.List;
import java.util.stream.Collectors;

public class JoinUtil<A, B> {

    private List<JoinResultWrapper<A, B>> result = Lists.newArrayList();

    public JoinUtil(List<A> listA, List<B> listB, Function2<A, B, Boolean> comparator) {
        this.result.clear();
        Iterator var4 = listA.iterator();

        while(var4.hasNext()) {
            A a = (A) var4.next();
            Iterator var6 = listB.iterator();

            while(var6.hasNext()) {
                B b = (B) var6.next();
                if ((Boolean)comparator.get(a, b)) {
                    this.result.add(new JoinResultWrapper(a, b));
                }
            }
        }

    }

    public <R> List<R> select(Function2<A, B, R> mapper) {
        return (List)this.result.stream().map((w) -> {
            return mapper.get(w.pa, w.pb);
        }).collect(Collectors.toList());
    }

    public <R> List<R> select(Function2<A, B, R> mapper, Function2<A, B, Boolean> predicate) {
        return (List)this.result.stream().filter((w) -> {
            return (Boolean)predicate.get(w.pa, w.pb);
        }).map((w) -> {
            return mapper.get(w.pa, w.pb);
        }).collect(Collectors.toList());
    }

}
